/**
 * 
 */
package logic;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jpa_objects.Message;
import jpa_objects.Player;
import persistence.DBCommonConsult;

/**
 * @author Alejandro Casas
 *
 */
public class Sorter {



	public static boolean addOrdered (List<Player> list, Player p)
	{
		boolean resp = false;
		int i = 0;
		for (Player player : list) {
			if(p.getMail().compareToIgnoreCase(player.getMail())>0)
			{
				
			}
			else if(p.getMail().compareToIgnoreCase(player.getMail())<0)
			{
				list.add(i,p);
				resp = true;
				break;
			}
			else if(player.getMail().compareToIgnoreCase(p.getMail())==0)
			{
				resp = false;
			}
			i++;
		}
		return resp;
	}


	public static void quickSort (List<Message> list, int lowIndex, int highIndex){

		//at least one item must exist in the array 
		if (lowIndex>=highIndex){
			return;
		}

		int pivotIndex=getMedianIndexAsPivotIndex(lowIndex, highIndex);
		//1) Choose pivot from the sublist
		Message pivot=list.get(pivotIndex);
		//System.out.println("Pivot : "+pivot);
		//2) Swap the pivot to the last item in the array
		swapItemsWithIndices(list, pivotIndex, highIndex); 	



		int i=lowIndex-1;
		int j=highIndex;

		do{ //Notice the <j (pivot item is ignored). We stop when both the counters cross

			//compareTo will return 0 when it reaches the pivot - will exit loop
			do {i++;} while (list.get(i).compareTo(pivot)<0);
			//we dont have the protection as the previous loop. 
			//So, add extra condition to prevent 'j' from overflowing outside the current sub array
			do {j--;} while (list.get(j).compareTo(pivot)>0 &&(j>lowIndex));

			if (i<j){
				swapItemsWithIndices(list, i, j);
			}
			System.out.println("I :"+i + " J :"+j);
		} while (i<j);

		swapItemsWithIndices(list, highIndex, i);//bring pivot to i's position	
		//System.out.println("Comparable array : "+Arrays.asList(list));
		for (int y = 0; y < list.size(); y++) {
			System.out.println("Comparable array : "+y+": "+list.get(y).getDate());
		}
		//the big subarray is partially sorted (agrees to invariant). Let's recurse and bring in more hands

		quickSort(list, lowIndex, i-1); //sort subarray between low index and one before the pivot
		quickSort(list, i+1, highIndex); //sort subarray between low index and one before the pivot
	}


	//... since swapping with array is the easiest way to swap two objects
	private static void swapItemsWithIndices(List<Message> list, int firstItem, int secondItem) {
		System.out.println("Swapping "+list.get(firstItem).getDate() +"  and  "+list.get(secondItem).getDate());
		final Message tempItem=list.get(firstItem);
		final Message temp2=list.get(secondItem);
		//list.get(firstItem)=list.get(secondItem);
		list.set(firstItem, temp2);


		//list.get(secondItem)=tempItem;
		list.set(secondItem, tempItem);

		for (int j = 0; j < list.size(); j++) {
			System.out.println("After swap array : "+j+": "+list.get(j).getDate());
		}
		//System.out.println("After swap array : "+Arrays.asList(list));
	}


	//Variation 1 - chose median as pivot  
	private static int getMedianIndexAsPivotIndex(int lowIndex, int highIndex) {
		return lowIndex+((highIndex-lowIndex)/2);
	}

	public static void main(String[] args) {

		System.out.println(""+(new Timestamp(( new java.util.Date()).getTime())));
		/*
		List<Message> sent=new ArrayList<Message>();
		boolean a=false;
		for (int i = 0; i < 10; i++) {
			if(a)
			{
				java.sql.Timestamp t=(new Timestamp(( new java.util.Date()).getTime()));
				t.setMonth(t.getMonth()+i);
				sent.add(new Message(""+t, "mailfrom","message"+i, "title", "mailTo"));
				a=false;
			}
			else
			{
				java.sql.Timestamp t=(new Timestamp(( new java.util.Date()).getTime()));
				t.setYear(t.getYear()+i);
				sent.add(new Message(""+t, "mailfrom","message"+i, "title", "mailTo"));
				a=true;
			}
		}


		long startTime = System.nanoTime();
		for (int j = 0; j < sent.size(); j++) {
			System.out.println("Original array : "+j+": "+sent.get(j).getDate());
		}

		quickSort(sent, 0, sent.size()-1);

		for (int j = 0; j < sent.size(); j++) {
			System.out.println("Final array : "+j+": "+sent.get(j).getDate());
		}
		System.out.println(System.nanoTime()-startTime);
		*/

	}




}


